package com.xindaoapp.happypet.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.XDHttpClient;
import com.xindaoapp.happypet.adapter.MyBaseAdapter;
import com.xindaoapp.happypet.bean.PetInfo;
import com.xindaoapp.happypet.bean.PetInfo.Pet;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.xdConfig;
import com.xindaoapp.happypet.view.CircleImageView;

public class OhterTab1Fragment extends XDBaseFragment {

    private String uid = "";

    private FrameLayout fl;

    private boolean firstSuccess = false;

    private ListView listView;

    PetListAdapter petAdapter;

    private Context mContext;

    private RelativeLayout noresult;

    private final List<Pet> mList = new ArrayList<Pet>();

    public static OhterTab1Fragment Instance(Bundle args) {

        final OhterTab1Fragment f = new OhterTab1Fragment();

        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uid = getArguments() != null ? getArguments().getString("uid") : "";
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return mView = inflater.inflate(R.layout.fragment_other_tab1, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    protected void initUi() {

        mContext = getActivity();

        listView = (ListView) findViewById(R.id.listView);
        noresult = (RelativeLayout) findViewById(R.id.noresult);

        petAdapter = new PetListAdapter(mContext, mList, R.layout.layout_pet_item);

        fl = (FrameLayout) findViewById(R.id.loading);

        getData();

    }

    private void getData() {

        if (CommonUtil.checkNetState(mContext)) {

            RequestParams params = new RequestParams();
            params.put("uid", uid);

            XDHttpClient.get(xdConfig.MYPETLIST, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, String content) {
                    if (!TextUtils.isEmpty(content)) {

                        if (!firstSuccess) {
                            firstSuccess = true;
                            fl.setVisibility(View.GONE);
                        }

                        PetInfo entity = null;
                        try {
                            entity = JSON.parseObject(content, PetInfo.class);
                            if (entity.getResult().equals("0")) {
                                mList.addAll(entity.getArray());
                                petAdapter.notifyDataSetChanged();
                                listView.setAdapter(petAdapter);
                                if (mList.size() == 0) {
                                    noresult.setVisibility(View.VISIBLE);
                                    listView.setVisibility(View.GONE);
                                }
                            } else {
                                Toast.makeText(mContext, "数据获取错误", Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                    }
                }

                @Override
                public void onFailure(Throwable arg0, String arg1) {
                    super.onFailure(arg0, arg1);
                    Toast.makeText(mContext, R.string.pLease_check_network, 0).show();
                }
            });
        } else {
            Toast.makeText(mContext, R.string.pLease_check_network, 0).show();
            return;
        }
    }

    class PetListAdapter extends MyBaseAdapter<Pet> {

        private final Context mContext;

        public PetListAdapter(Context context, List<Pet> mList, int resId) {
            super(context, mList, resId);
            this.mContext = context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent, Pet t) {
            ViewHolder holder = (ViewHolder) convertView.getTag();
            if (null == holder) {
                holder = new ViewHolder();
                holder.pet_icon = (CircleImageView) convertView.findViewById(R.id.pet_icon);
                holder.tv_peg_name = (TextView) convertView.findViewById(R.id.tv_peg_name);
                holder.tv_peg_type = (TextView) convertView.findViewById(R.id.tv_peg_type);
                holder.tv_peg_type2 = (TextView) convertView.findViewById(R.id.tv_peg_type2);
                holder.iv_peg_sex = (ImageView) convertView.findViewById(R.id.iv_peg_sex);
                convertView.setTag(holder);
            }
            String str;

            ImageLoader.getInstance().displayImage(t.getCover(), holder.pet_icon);

            holder.tv_peg_name.setText(t.getTitle());

            holder.tv_peg_type2.setText(t.getBreed());

            str = t.getSex();

            if (str.equals("2")) {
                holder.iv_peg_sex.setBackgroundResource(R.drawable.icon_women);
            } else {
                holder.iv_peg_sex.setBackgroundResource(R.drawable.icon_men);
            }

            str = t.getPettype();

            if (str.equals("1")) {
                holder.tv_peg_type.setText("汪星人");
            }
            if (str.equals("2")) {
                holder.tv_peg_type.setText("喵星人");
            }
            if (str.equals("3")) {
                holder.tv_peg_type.setText("小宠星人");
            }
            if (str.equals("4")) {
                holder.tv_peg_type.setText("水星人");
            }

            return convertView;
        }

        class ViewHolder {
            CircleImageView pet_icon;
            ImageView iv_peg_sex;
            TextView tv_peg_name;
            TextView tv_peg_type;
            TextView tv_peg_type2;
        }

    }

}
